//
//  vistorLoginView.swift
//  XJWeiBo
//
//  Created by 徐健 on 16/3/8.
//  Copyright © 2016年 徐健. All rights reserved.
//

import UIKit
import SnapKit


@objc protocol vistorLoginViewDelegate:NSObjectProtocol{
    //协议方法
    optional func userWillRegister()
    
    //登录协议方法
    func userWillLogin()
}

class vistorLoginView: UIView {
    
    
    
    //声明代理对象
    weak var vistorDelegate:vistorLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func setupInforWith(title:String,imageName:String?){
        tipLable.text = title
        if imageName != nil{
           circleIcon.image = UIImage(named:imageName!)
            
            largeHouse.hidden = true
            
            bringSubviewToFront(circleIcon)
        }else{
            startAnimation()
        }
        
    }
    
    
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 10
        
        animation.toValue = 2 * M_PI
        
        animation.removedOnCompletion = false
        
        circleIcon.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    
    private func setUpUI(){
        addSubview(circleIcon)
        
        addSubview(maskImage)
        
        addSubview(largeHouse)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //VFL使用父视图添加约束对象
        //禁用frame布局（手写代码添加自动布局时候）
//        
//        circleIcon.translatesAutoresizingMaskIntoConstraints = false
//        
//        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterX, relatedBy:.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        self.addConstraint(NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//————————————————————————————————————————————————————————————————————————————
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
            
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLable.snp_left)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLable.snp_right)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        maskImage.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(CGSize.init(width: self.bounds.width, height: self.bounds.height))
            
            make.center.equalTo(circleIcon.snp_center)
        }
        
        
        
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents:.TouchUpInside)
        
        
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        
        
        

        let value:CGFloat = 236.0/255.0

        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
    }
    
    
    func loginBtnClick(){
        print(__FUNCTION__)
        vistorDelegate?.userWillLogin()
    }
    
    
    func registerBtnClick(){
        print(__FUNCTION__)
        vistorDelegate?.userWillRegister?()
    }
    
    
    
    //遮罩
    private lazy var maskImage:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    private lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var tipLable:UILabel = UIFactory.lableFactory(UIColor.darkGrayColor(), text: "", fontSize: 14)
  
    
    
    private lazy var loginBtn:UIButton = UIFactory.buttonWithTitle("登录", backImageName: "common_button_white_disable", textColor:UIColor.darkGrayColor(), fontSize: 14)

    
    private lazy var registerBtn:UIButton = UIFactory.buttonWithTitle("注册", backImageName: "common_button_white_disable", textColor:themeColor, fontSize: 14)

    

}
